package com.elitel.frame.base.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.elitel.frame.base.service.SysPrjMenuService;
import com.elitel.frame.main.dao.SysPrjmenuMapper;
import com.elitel.frame.main.dao.ext.ExtSysPageuiMapper;
import com.elitel.frame.main.dao.ext.ExtSysPrjmenuMapper;
import com.elitel.frame.main.entity.SysPageui;
import com.elitel.frame.main.entity.SysPrjmenu;
import com.elitel.frame.main.entity.ext.SysPrjmenuExt;
import com.elitel.frame.main.entity.vo.PageRequestVo;
import com.elitel.frame.main.entity.vo.PageResponseVo;

@Service
public class SysPrjMenuServiceImpl implements SysPrjMenuService{
	
	@Autowired
	private SysPrjmenuMapper sysPrjmenuMapper;
	@Autowired
	private ExtSysPrjmenuMapper extSysPrjmenuMapper;
	@Autowired
	private ExtSysPageuiMapper extSysPageuiMapper;
	
	@Override
	public PageResponseVo<SysPrjmenu> getPrjMenuList(PageRequestVo<SysPrjmenu> pr) {
		pr.StartPage();
		PageResponseVo<SysPrjmenu> responseVo = new PageResponseVo<SysPrjmenu>();
		List<SysPrjmenu> list = extSysPrjmenuMapper.getPrjMenuByPagination(pr);
		Long count = extSysPrjmenuMapper.getCountByPagination(pr);
		responseVo.setRows(list);
		responseVo.setTotal(count.intValue());
		return responseVo;
	}

	@Override
	public void insertPrjMenuConfig(SysPrjmenu sysPrjmenu) {
		sysPrjmenu.setGuid(UUID.randomUUID().toString());
		sysPrjmenuMapper.insertSelective(sysPrjmenu);
	}

	@Override
	public SysPrjmenuExt getPrjMenuByGuid(String guid) {
		SysPrjmenuExt prjMenu = extSysPrjmenuMapper.selectByPrimaryKey(guid);
		return prjMenu;
	}

	@Override
	public void updatePrjMenuConfig(SysPrjmenu sysPrjmenu) {
		sysPrjmenuMapper.updateByPrimaryKeySelective(sysPrjmenu);
		
	}

	@Override
	public void deletePrjMenuConfig(String guids) {
		List<String> list = new ArrayList<String>();
		String[] idArray = guids.split(",");
		for (int i = 0; i < idArray.length; i++) {
			list.add(idArray[i]);
		}
		extSysPrjmenuMapper.deletePrjMenuConfig(list);
		
	}

	@Override
	public List<SysPrjmenu> getPrjMenuByMenucode(String menucode,String pcode) {
		List<SysPrjmenu> list = extSysPrjmenuMapper.getPrjMenuByMenucode(menucode,pcode);
		return list;
	}

	@Override
	public List<SysPrjmenuExt> getMenuList(String pcode,String parent) {
		List<SysPrjmenuExt> rlist = new ArrayList<SysPrjmenuExt>();
		List<SysPrjmenuExt> flist = extSysPrjmenuMapper.getMenuList(pcode, parent);
		if(flist.size() > 0){
			for (SysPrjmenuExt prjext : flist) {
				rlist.add(prjext);
				rlist.addAll(getMenuList(pcode, prjext.getGuid()));
			}
		}
		return rlist;
	}

	@Override
	public List<SysPrjmenu> getMenuByParent(String guid) {
		List<SysPrjmenu> list = extSysPrjmenuMapper.getMenuByParent(guid);
		return list;
	}

	@Override
	public List<SysPageui> getPagebyPcode(String pcode) {
		List<SysPageui> list = extSysPageuiMapper.getPagebyPcode(pcode);
		return list;
	}

}
